/**
 * Created by charsen on 16/5/26.
 */
directives.directive('fieldManager', ["$rootScope", "tools", "$timeout", function ($rootScope, tools, $timeout) {
    return {
        restrict: 'AE',
        scope: {
            menu: '=json'
        },
        replace: true,
        link: function (scope, element, attr) {
            var selected_type = '';
            var events = {};

            scope.fields = {
                dimensionality2: [],
                dimensionality: [],
                measurement2: [],
                measurement: [],
                condition: [],
                distance:'',
                intervalPeriodUnit: {
                    name:'月',
                    val :'month'
                }
            };

            scope.dimensionality2_shown = false;
            scope.dimensionality_shown = true;
            scope.measurement2_shown = false;
            scope.measurement_shown = true;
            scope.condition_shown = false;
            scope.distance_shown = false;
            scope.intervalPeriodUnit_shown = false;

            // 鼠标释放的时候
            var targetIndex = -1;
            var targetType = "";
            $(element).find('.dropdown.categoryList').dropdown({});
            scope.setdistance = function(distance){
                scope.fields.distance = distance;
                $rootScope.$broadcast('DISTANCE_ADD_EV', distance);
            }

            scope.swithIntervalPeriodUnit = function(intervalPeriodUnit,intervalPeriodUnitname){
                scope.fields.intervalPeriodUnit = {
                    val:intervalPeriodUnit,
                    name:intervalPeriodUnitname

                };
              
                $rootScope.$broadcast('INTERVALPERIODUNIT_ADD_EV',scope.fields.intervalPeriodUnit)
            }

            // 记住
            $rootScope.setDraggedField = function (type, index) {
                targetIndex = index;
                targetType = type;
            }

            // 维度左右交换
            $(element).find('.fields_for_dimensionality')[0].addEventListener('drop', function (ev) {
                var data = JSON.parse(ev.dataTransfer.getData("Text"));
                if (data.type == targetType && data.index == targetIndex) {
                    return;
                }

                $timeout(function () {
                    scope.fields.dimensionality.splice(targetIndex, 0, scope.fields.dimensionality.splice(data.index, 1)[0]);
                    $rootScope.$broadcast('DIMENSION_RECOVER_EV', scope.fields.dimensionality);
                }, 0);
                ev.preventDefault();
            });

            // 第二维度左右交换
            $(element).find('.fields_for_dimensionality2')[0].addEventListener('drop', function (ev) {
                var data = JSON.parse(ev.dataTransfer.getData("Text"));
                if (data.type == targetType && data.index == targetIndex) {
                    return;
                }

                $timeout(function () {
                    scope.fields.dimensionality2.splice(targetIndex, 0, scope.fields.dimensionality2.splice(data.index, 1)[0]);
                    $rootScope.$broadcast('DIMENSION2_RECOVER_EV', scope.fields.dimensionality2);
                }, 0);
                ev.preventDefault();
            });

            // 度量左右交换
            $(element).find('.fields_for_measurement')[0].addEventListener('drop', function (ev) {
                var data = JSON.parse(ev.dataTransfer.getData("Text"));
                if (data.type == targetType && data.index == targetIndex) {
                    return;
                }
                $timeout(function () {
                    scope.fields.measurement.splice(targetIndex, 0, scope.fields.measurement.splice(data.index, 1)[0]);
                    $rootScope.$broadcast('MEASUREMENT_RECOVER_EV', scope.fields.measurement);
                }, 0);
                ev.preventDefault();
            });

            var getFieldByTypeAndColumnId = function (type, columnId) {
                if (selected_type === 'detailtab') {
                        type = 'dimensionality';
                    }
                var list = scope.fields[type];
                for (var i = 0; i < list.length; i++) {
                    if (list[i].columnId == columnId) {
                        return {
                            field: list[i],
                            index: i
                        }
                    }
                }
            }

            scope.dimensionality2_name = "维度";
            scope.dimensionality_name = "维度";
            scope.measurement2_name = "度量";
            scope.measurement_name = "度量";
            scope.condition_name = "维度";

            var set_as_second_y_menu_item = {
                name: '设置为双Y轴线',
                fn: function (type, columnId) {
                    var field = getFieldByTypeAndColumnId(type, columnId).field;

                    if (field.setAsSecondY) {
                        field.setAsSecondY = false;
                        field.submenus[2].name = '设置为双Y轴线';
                    } else {
                        field.setAsSecondY = true;
                        field.submenus[2].name = '取消双Y轴线';
                    }

                    angular.forEach(scope.fields.measurement, function (mea) {
                        if (mea.columnId != columnId) {
                            mea.setAsSecondY = false;
                            mea.submenus[2].name = '设置为双Y轴线';
                        }
                    });

                    $rootScope.$broadcast('MEASUREMENT_RECOVER_EV', scope.fields.measurement);

                    $timeout(function () {
                    }, 0);

                    $rootScope.$broadcast('FIELD_ITEM_DROP_OFF');
                }
            };
            var set_as_main_sort_menu_item = {
                name: '设为主排序',
                submenus: [
                    {
                        name: '升序',
                        fn: function (type, columnId) {
                            var field = getFieldByTypeAndColumnId(type, columnId).field;
                            if (selected_type === 'detailtab') {
                                $rootScope.$broadcast('MAIN_SORT_EV', field.fieldName, 'asc');
                            } else {
                                $rootScope.$broadcast('MAIN_SORT_EV', field.meaType + '_' + field.fieldName, 'asc');
                            }
                            
                            $rootScope.$broadcast('FIELD_ITEM_DROP_OFF');
                        }
                    },
                    {
                        name: '降序',
                        fn: function (type, columnId) {
                            var field = getFieldByTypeAndColumnId(type, columnId).field;
                            if (selected_type === 'detailtab') {
                                $rootScope.$broadcast('MAIN_SORT_EV', field.fieldName, 'desc');
                            } else {
                                $rootScope.$broadcast('MAIN_SORT_EV', field.meaType + '_' + field.fieldName, 'desc');
                            }
                            
                            $rootScope.$broadcast('FIELD_ITEM_DROP_OFF');
                        }
                    }
                ]
            };
            var measure_menu_item = {
                name: '度量',
                submenus: [
                    {
                        name: '总和',
                        fn: function (type, columnId) {
                            var field = getFieldByTypeAndColumnId(type, columnId).field;
                            field.meaType = 'sum';
                            $rootScope.$broadcast('MEA_CHANGE_EV', field.fieldId, 'sum');
                            $rootScope.$broadcast('FIELD_ITEM_DROP_OFF');
                        }
                    }, {
                        name: '平均',
                        fn: function (type, columnId) {
                            var field = getFieldByTypeAndColumnId(type, columnId).field;
                            $timeout(function () {
                                field.meaType = 'avg';
                            }, 0);
                            $rootScope.$broadcast('MEA_CHANGE_EV', field.fieldId, 'avg');
                            $rootScope.$broadcast('FIELD_ITEM_DROP_OFF');
                        }
                    }, {
                        name: '计数',
                        fn: function (type, columnId) {
                            var field = getFieldByTypeAndColumnId(type, columnId).field;
                            field.meaType = 'count';
                            $rootScope.$broadcast('MEA_CHANGE_EV', field.fieldId, 'count');
                            $rootScope.$broadcast('FIELD_ITEM_DROP_OFF');
                        }
                    }, {
                        name: '最大值',
                        fn: function (type, columnId) {
                            var field = getFieldByTypeAndColumnId(type, columnId).field;
                            field.meaType = 'max';
                            $rootScope.$broadcast('MEA_CHANGE_EV', field.fieldId, 'max');
                            $rootScope.$broadcast('FIELD_ITEM_DROP_OFF');
                        }
                    }, {
                        name: '最小值',
                        fn: function (type, columnId) {
                            var field = getFieldByTypeAndColumnId(type, columnId).field;
                            field.meaType = 'min';
                            $rootScope.$broadcast('MEA_CHANGE_EV', field.fieldId, 'min');
                            $rootScope.$broadcast('FIELD_ITEM_DROP_OFF');
                        }
                    }
                ]
            };
            var remove_menu_item = {
                name: '移除',
                fn: function (type, columnId) {
                    // 明细表的维度和度量统一存放在维度里
                    if (selected_type === 'detailtab') {
                        type = 'dimensionality';
                    }
                    var field = getFieldByTypeAndColumnId(type, columnId);
                    if (field) {
                        scope.fields[type].splice(field.index, 1);
                    }
                    switch (type) {
                        case 'measurement':
                            $rootScope.$broadcast('MEASURE_REDUCE_EV', columnId);
                            break;
                        case 'measurement2':
                            $rootScope.$broadcast('MEASURE2_REDUCE_EV', columnId);
                            break;
                        case 'dimensionality':
                            $rootScope.$broadcast('DIMENSION_REDUCE_EV', columnId);
                            break;
                        case 'dimensionality2':
                            $rootScope.$broadcast('DIMENSION2_REDUCE_EV', columnId);
                            break;
                        case 'condition':
                            $rootScope.$broadcast('CONDITION_REDUCE_EV', columnId);
                            break;
                    }

                    $rootScope.$broadcast('FIELD_ITEM_DROP_OFF');
                    $timeout(function () {
                    }, 0);
                }
            };
            // 从字段列表接收字段
            events.FIELD_EV_OFF = $rootScope.$on('FIELD_EV', function (e, type, field, resource) {
                // 为组件字段设置上下文菜单
                if (type == 'condition') {
                    field.submenus = [remove_menu_item];
                }

                // 为表字段设置上下文菜单
                if (selected_type == 'summarytab') {
                    if (type == 'measurement') {
                        field.submenus = [measure_menu_item, set_as_main_sort_menu_item, remove_menu_item];
                    }
                    if (type == 'dimensionality') {
                        field.submenus = [remove_menu_item];
                    }
                }
                else if (selected_type == 'crosstab') {
                    if (type == 'measurement') {
                        field.submenus = [measure_menu_item, set_as_main_sort_menu_item, remove_menu_item];
                    }
                    if (type == 'dimensionality') {
                        field.submenus = [remove_menu_item];
                    }
                    if (type == 'dimensionality2') {
                        field.submenus = [remove_menu_item];
                    }
                }
                else if (selected_type == 'detailtab') {
                    if (type == 'measurement' || type =='dimensionality') {
                        field.submenus = [set_as_main_sort_menu_item, remove_menu_item];
                    }
                }
                else if (selected_type == 'retentionrate') {
                    if (type =='dimensionality' || type == 'dimensionality2') {
                        field.submenus = [remove_menu_item];
                    }
                }

                // 为图字段设置上下文
                if (selected_type == 'pie' ||selected_type=='circularPie' || selected_type == 'nestedPie' || selected_type == 'line' || selected_type == 'verBar' || selected_type == 'area' || selected_type == 'histogram') {
                    if (type == 'dimensionality') {
                        field.submenus = [remove_menu_item];
                    }
                    else if (type == 'measurement') {
                        field.submenus = [measure_menu_item, set_as_main_sort_menu_item, remove_menu_item];
                        
                    }
                }
                else if (selected_type == 'bar') {
                    if (type == 'dimensionality') {
                        field.submenus = [remove_menu_item];
                    }
                    else if (type == 'measurement') {
                        field.submenus = [measure_menu_item, set_as_main_sort_menu_item, set_as_second_y_menu_item, remove_menu_item];
                    }
                }
                else if (selected_type == 'waterfall') {
                    if (type == 'dimensionality') {
                        field.submenus = [remove_menu_item];
                    }
                    else if (type == 'measurement') {
                        field.submenus = [remove_menu_item];
                    }
                }
                else if (selected_type == 'bubble' || selected_type == 'scatter' || selected_type == 'radar' || selected_type == 'funnel' || selected_type == 'treemap' || selected_type == 'provinceMap' || selected_type == 'cityMap') {
                    if (type == 'dimensionality') {
                        field.submenus = [remove_menu_item];
                    }
                    else if (type == 'measurement') {
                        field.submenus = [remove_menu_item];
                    }
                    else if (type == 'measurement2') {
                        field.submenus = [remove_menu_item];
                    }
                }
                else if (selected_type == 'dashboard') {
                    if (type == 'measurement') {
                        field.submenus = [measure_menu_item, remove_menu_item];
                    }
                } 
                else if (selected_type == 'heatmap') {
                    if (type == 'measurement') {
                        field.submenus = [measure_menu_item, set_as_main_sort_menu_item, remove_menu_item];
                    }
                    if (type == 'dimensionality') {
                        field.submenus = [remove_menu_item];
                    }
                    if (type == 'dimensionality2') {
                        field.submenus = [remove_menu_item];
                    }
                } 
                else if(selected_type == 'heatmarketmap'){
                    if (type == 'dimensionality') {
                        field.submenus = [remove_menu_item];
                    }
                    if (type == 'dimensionality2') {
                        field.submenus = [remove_menu_item];
                    }
                }

                if(resource == 'add'){
                // 只能有一个度量的图类型
                    var onlyOneMeaTypes = ['pie','circularPie', 'nestedPie', 'histogram', 'treemap', 'dashboard', 'provinceMap', 'cityMap','heatmap'];

                    if (type == 'condition' && scope.fields[type].length) {
                        $rootScope.$broadcast('DIMENSION_REDUCE_EV', scope.fields[type][0].columnId); // 组件只能有一个字段
                        scope.fields[type][0] = field;
                    }
                    else if (selected_type == 'crosstab' && type == 'measurement' && scope.fields[type].length) {
                        $rootScope.$broadcast('MEASURE_REDUCE_EV', scope.fields[type][0].columnId); // 交叉表只能有一个度量
                        scope.fields[type][0] = field;
                    }
                    else if (onlyOneMeaTypes.indexOf(selected_type) != -1 && type == 'measurement' && scope.fields[type].length) {
                        $rootScope.$broadcast('MEASURE_REDUCE_EV', scope.fields[type][0].columnId); // 饼图只能有一个度量
                        scope.fields[type][0] = field;
                    }
                    else {
                        scope.fields[type].push(field);
                    }

                    // if(selected_type == 'retentionrate') {
                    //     if(type =='dimensionality') {
                    //         $rootScope.$broadcast('DIMENSION_REDUCE_EV', scope.fields['dimensionality'][0].columnId); 
                    //         scope.fields['dimensionality'][0] = field;
                    //     } else if(type == 'dimensionality2'){
                    //         $rootScope.$broadcast('DIMENSION2_REDUCE_EV', scope.fields['dimensionality2'][0].columnId); 
                    //         scope.fields['dimensionality2'][0] = field;
                    //     }
                        
                    // }

                    // 只能有一个维度的图类型
                    // var onlyOneDimTypes = ['heatmap','heatmarketmap'];

                    // if (onlyOneMeaTypes.indexOf(selected_type) != -1 && type == 'dimensionality' && scope.fields[type].length) {
                    //     $rootScope.$broadcast('DIMENSION_REDUCE_EV', scope.fields[type][0].columnId); // 饼图只能有一个度量
                    //     scope.fields[type][0] = field;
                    // }
                    // else {
                    //     scope.fields[type].push(field);
                    // }

                    // // 只能有一个维度2的图类型
                    // var onlyOneDimTypes = ['heatmap','heatmarketmap'];

                    // if (onlyOneMeaTypes.indexOf(selected_type) != -1 && type == 'dimensionality2' && scope.fields[type].length) {
                    //     $rootScope.$broadcast('DIMENSION2_REDUCE_EV', scope.fields[type][0].columnId); // 饼图只能有一个度量
                    //     scope.fields[type][0] = field;
                    // }
                    // else {
                    //     scope.fields[type].push(field);
                    // }

                    // 发送字段给正在被选中的组件
                    switch (type) {
                        case 'dimensionality':
                            $rootScope.$broadcast('DIMENSION_ADD_EV', field);
                            break;
                        case 'dimensionality2':
                            $rootScope.$broadcast('DIMENSION2_ADD_EV', field);
                            break;
                        case 'measurement':
                            $rootScope.$broadcast('MEASURE_ADD_EV', field);
                            break;
                        case 'measurement2':
                            $rootScope.$broadcast('MEASURE2_ADD_EV', field);
                            break;
                        case 'condition':
                            $rootScope.$broadcast('CONDITION_ADD_EV', field);
                            break;
                    }
                }
                

                $timeout(function () {
                }, 0);
            });

            // 监听从图发出来的字段恢复事件
            events.FIELD_RECOVER_FROM_CHART_EV_OFF = $rootScope.$on('FIELD_RECOVER_FROM_CHART_EV', function (e, dimensionality,dimensionality2, measurement, measurement2, classifies, type,distance) {
                selected_type = type;

                // 采用复制的形式,避免在此处对队列的修改会影响到对应的组件的队列
                
                $timeout(function(){
                    angular.forEach(dimensionality,function(dim){
                        $rootScope.$broadcast('FIELD_EV','dimensionality',dim,'resave');

                    })
                    angular.forEach(dimensionality2,function(dim){
                        $rootScope.$broadcast('FIELD_EV','dimensionality2',dim,'resave');

                    })
                    angular.forEach(measurement,function(dim){
                        $rootScope.$broadcast('FIELD_EV','measurement',dim,'resave');

                    })
                    angular.forEach(measurement2,function(dim){
                        $rootScope.$broadcast('FIELD_EV','measurement2',dim,'resave');

                    })
                    scope.fields.dimensionality = angular.copy(dimensionality);
                    scope.fields.dimensionality2 = angular.copy(dimensionality2);
                    scope.fields.measurement = angular.copy(measurement);
                    scope.fields.measurement2 = angular.copy(measurement2);
                    scope.fields.distance = angular.copy(distance);
                    scope.distance = angular.copy(distance);
                },0)
               

                // 增加维度和度量的下拉菜单
                // angular.forEach(scope.fields.dimensionality, function (dim) {
                //     // dim.submenus = angular.copy(submenus);
                //     // dim.submenus[0].hide = true;
                //     // dim.submenus[1].hide = true;
                //     // dim.submenus[2].hide = true;
                // });
                //
                // angular.forEach(scope.fields.measurement, function (mea) {
                //     // mea.submenus = angular.copy(submenus);
                //     // if (mea.setAsSecondY) {
                //     //     mea.submenus[2].name = '取消双Y轴线';
                //     // }
                // });

                scope.dimensionality_name = "维度";
                scope.dimensionality2_name = "纬度";
                scope.measurement_name = "度量";
                scope.measurement2_name = "气泡";

                if (type == 'funnel') {
                    $timeout(function () {
                        scope.dimensionality2_shown = false;
                        scope.dimensionality_shown = false;
                        scope.condition_shown = false;
                        scope.measurement_shown = true;
                        scope.measurement2_shown = false;
                        scope.distance_shown = false;
                        scope.intervalPeriodUnit_shown = false;
                    }, 0);
                } else if (type == 'bubble') {
                    $timeout(function () {
                        scope.dimensionality2_shown = false;
                        scope.dimensionality_shown = true;
                        scope.measurement_shown = true;
                        scope.measurement2_shown = true;
                        scope.condition_shown = false;
                        scope.distance_shown = false;
                        scope.intervalPeriodUnit_shown = false;
                    }, 0);
                } else if(type == 'histogram'){
                    $timeout(function () {
                        scope.dimensionality2_shown = false;
                        scope.dimensionality_shown = true;
                        scope.distance_shown = true;
                        scope.condition_shown = false;
                        scope.measurement_shown = true;
                        scope.measurement2_shown = false;
                        scope.intervalPeriodUnit_shown = false;
                    }, 0);
                } else if(type == 'heatmap'){
                    $timeout(function () {
                        scope.dimensionality_name = "经度";
                        scope.dimensionality2_name = "纬度";
                        scope.measurement_name = "度量";
                        scope.dimensionality2_shown = true;
                        scope.dimensionality_shown = true;
                        scope.distance_shown = false;
                        scope.condition_shown = false;
                        scope.measurement_shown = true;
                        scope.measurement2_shown = false;
                        scope.intervalPeriodUnit_shown = false;
                    }, 0);
                }else if(type == 'heatmarketmap'){
                    $timeout(function () {
                        scope.dimensionality_name = "经度";
                        scope.dimensionality2_name = "纬度";
                        scope.dimensionality2_shown = true;
                        scope.dimensionality_shown = true;
                        scope.distance_shown = false;
                        scope.condition_shown = false;
                        scope.measurement_shown = false;
                        scope.measurement2_shown = false;
                        scope.intervalPeriodUnit_shown = false;
                    }, 0);
                } else {
                    $timeout(function () {
                        scope.dimensionality2_shown = false;
                        scope.dimensionality_shown = true;
                        scope.measurement_shown = true;
                        scope.measurement2_shown = false;
                        scope.condition_shown = false;
                        scope.distance_shown = false;
                        scope.intervalPeriodUnit_shown = false;
                    }, 0);
                }

                $timeout(function () {
                    $('.chart-manager-container').height(document.documentElement.clientHeight - $('.editor-header').outerHeight() - $('.editor-shortcut').outerHeight() - $('.field-manager-wrap').outerHeight() - 25);
                }, 50);
            });
            // 监听从表发出来的字段恢复事件
            events.FIELD_RECOVER_FROM_SHEET_EV_OFF = $rootScope.$on('FIELD_RECOVER_FROM_SHEET_EV', function (e, dimensionality, dimensionality2, measurement, type, intervalPeriodUnit) {
                $timeout(function(){
                    selected_type = type;
                    angular.forEach(dimensionality,function(dim){
                        $rootScope.$broadcast('FIELD_EV','dimensionality',dim,'resave');
                    })
                    angular.forEach(measurement,function(dim){
                        $rootScope.$broadcast('FIELD_EV','measurement',dim,'resave');
                    })
                    angular.forEach(dimensionality2,function(dim){
                        $rootScope.$broadcast('FIELD_EV','dimensionality2',dim,'resave');

                    })
                    
                    scope.fields.measurement = angular.copy(measurement);
                    scope.fields.dimensionality = angular.copy(dimensionality);
                    scope.fields.dimensionality2 = angular.copy(dimensionality2);
                    scope.fields.intervalPeriodUnit = angular.copy(intervalPeriodUnit);
                   

                    
                    //scope.intervalPeriodUnit = angular.copy(intervalPeriodUnit);
                },0)
                
                // angular.forEach(scope.fields.dimensionality, function (dim) {
                //     //dim.submenus = angular.copy(submenus);
                //     //dim.submenus[0].hide = true;
                //     //dim.submenus[1].hide = true;
                // });
                //
                // angular.forEach(scope.fields.dimensionality2, function (dim) {
                //     //dim.submenus = submenus;
                // });
                //
                // angular.forEach(scope.fields.measurement, function (mea) {
                //     //mea.submenus = submenus;
                // });

                if (type == 'summarytab') {
                    $timeout(function () {
                        scope.dimensionality2_shown = false;
                        scope.dimensionality_shown = true;
                        scope.measurement_shown = true;
                        scope.condition_shown = false;
                        scope.distance_shown = false;
                        scope.intervalPeriodUnit_shown = false;

                        scope.dimensionality_name = "表头";
                        scope.measurement_name = '汇总';
                    }, 0);
                }

                if (type == 'crosstab') {
                    $timeout(function () {
                        scope.dimensionality2_shown = true;
                        scope.dimensionality_shown = true;
                        scope.measurement_shown = true;
                        scope.condition_shown = false;
                        scope.distance_shown = false;
                        scope.intervalPeriodUnit_shown = false;

                        scope.dimensionality2_name = "列表头";
                        scope.dimensionality_name = "行表头";
                        scope.measurement_name = '汇总';
                    }, 0);
                }

                if (type == 'detailtab') {
                    $timeout(function () {
                        scope.dimensionality2_shown = false;
                        scope.dimensionality_shown = true;
                        scope.measurement_shown = false;
                        scope.condition_shown = false;
                        scope.distance_shown = false;
                        scope.intervalPeriodUnit_shown = false;
                        scope.dimensionality_name = "维度/度量";
                    }, 0);
                }

                if (type == 'retentionrate') {
                    $timeout(function () {
                        scope.dimensionality2_shown = true;
                        scope.dimensionality_shown = true;
                        scope.measurement_shown = false;
                        scope.condition_shown = false;
                        scope.distance_shown = false;
                        scope.intervalPeriodUnit_shown = true;

                        scope.dimensionality_name = "日期";
                        scope.dimensionality2_name = "用户标志";
                    }, 0);
                }

                $timeout(function () {
                    $('.chart-manager-container').height(document.documentElement.clientHeight - $('.editor-header').outerHeight() - $('.editor-shortcut').outerHeight() - $('.field-manager-wrap').outerHeight() - 25);
                }, 50);
            });

            // 监听从组件发出来的字段恢复事件
            events.FIELD_RECOVER_FROM_COND_EV_OFF = $rootScope.$on('FIELD_RECOVER_FROM_COND_EV', function (e, column) {
                $timeout(function () {
                    scope.dimensionality2_shown = false;
                    scope.dimensionality_shown = false;
                    scope.measurement_shown = false;
                    scope.condition_shown = true;
                    scope.distance_shown = false;

                    if (column) {
                        scope.fields.condition[0] = column;
                        angular.forEach(scope.fields.condition,function(dim){
                            $rootScope.$broadcast('FIELD_EV','condition',dim,'resave');

                        })
                       
                        //scope.fields.condition[0].submenus = angular.copy(submenus);
                        //scope.fields.condition[0].submenus[0].hide = true;
                        //scope.fields.condition[0].submenus[1].hide = true;
                    } else {
                        scope.fields.condition.length = 0;
                    }
                }, 0);

                $timeout(function () {
                    $('.chart-manager-container').height(document.documentElement.clientHeight - $('.editor-header').outerHeight() - $('.editor-shortcut').outerHeight() - $('.field-manager-wrap').outerHeight() - 25);
                }, 50);
            });

            // 字段清空事件,来自于文本等一些组件
            events.FIELD_CLEAR_FROM_OBJECTS_OFF = $rootScope.$on('FIELD_CLEAR_FROM_OBJECTS', function () {
                scope.fields.dimensionality.length = scope.fields.dimensionality2.length = scope.fields.measurement.length = scope.fields.condition.length = scope.fields.distance = 0;
            });

            // 监听当前是哪一种类型的图或者表或者组件
            events.CHART_TYPE_EV_OFF = $rootScope.$on('CHART_TYPE_EV', function (e, type) {
                if (type == 'chart') {

                }
            });

            scope.$on('$destroy', function () {
                for (var i in events) {
                    if (i.indexOf('_OFF') != -1) {
                        events[i]();
                    }
                }
            });
        },
        templateUrl: "/trend-new-BI/app/components/common/chartManager/fieldManager/fieldManagerView.html"
    }
}]);